import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/views/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/login-gz.vue'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  //   {
  //     path: '/',
  //     // redirect: '/cms',
  //     // component: () => import('@/views/cms/index'),
  //     component: resolve => require(['@/views/cms/index'], resolve),
  //     hidden: true
  //   },
  {
    path: '/cms',
    // redirect: '/cms',
    // component: () => import('@/views/cms/index'),
    // component: resolve => require(['@/views/cms/index'], resolve),
    redirect: '/cms/welcome',
    hidden: true
  },
  {
    path: '/',
    hidden: true,
    redirect: '/cms/welcome'
  },
  {
    path: '/cms/welcome',
    component: resolve => require(['@/views/cms/index'], resolve),
    // component: resolve => require(['@/views/cms/welcome'], resolve),
    hidden: true
  },
  {
    path: '/cms/device-data',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'detail',
        component: (resolve) => require(['@/views/cms/realtime-monitor/pcsc/components/deviceDetail'], resolve),
        name: 'detail',
        meta: { title: '设备详情' }
      },
      {
        path: 'deviceInfoDetail',
        component: (resolve) => require(['@/views/cms/realtime-monitor/pcsc/components/deviceInfoDetail'], resolve),
        name: 'deviceInfoDetail',
        meta: { title: '边设备详情' }
      },
      {
        path: 'meteorologicalMonitor',
        component: (resolve) => require(['@/views/cms/realtime-monitor/monitor6c/meteorologicalMonitor'], resolve),
        name: 'meteorologicalMonitor',
        meta: { title: '气象监测' }
      },
      {
        path: 'iceCream',
        component: (resolve) => require(['@/views/cms/realtime-monitor/monitor6c/iceCream'], resolve),
        name: 'iceCream',
        meta: { title: '结冰实时数据' }
      },
      {
        path: 'temperature',
        component: (resolve) => require(['@/views/cms/realtime-monitor/monitor6c/temperature'], resolve),
        name: 'temperature',
        meta: { title: '电连接线夹温度监测' }
      },
      {
        path: 'iceImages',
        component: (resolve) => require(['@/views/cms/realtime-monitor/monitor6c/iceImages'], resolve),
        name: 'iceImages',
        meta: { title: '跨线桥监测（AI摄像头）' }
      },
      {
        path: 'displacement',
        component: (resolve) => require(['@/views/cms/realtime-monitor/monitor6c/displacement'], resolve),
        name: 'displacement',
        meta: { title: '接触网腕臂偏移量监测' }
      },
      {
        path: 'distance',
        component: (resolve) => require(['@/views/cms/realtime-monitor/monitor6c/distance'], resolve),
        name: 'distance',
        meta: { title: '接触网补偿装置A/B值监测' }
      },
      {
        path: 'inclinationVibration',
        component: (resolve) => require(['@/views/cms/realtime-monitor/monitor6c/inclinationVibration'], resolve),
        name: 'displacement',
        meta: { title: '接触网钢柱状态监测' }
      },
      {
        path: 'deviceInfo',
        component: (resolve) => require(['@/views/cms/realtime-monitor/pcsc/device-info'], resolve),
        name: 'deviceInfo',
        meta: { title: '子设备列表' }
      },
      {
        path: 'device',
        component: (resolve) => require(['@/views/cms/realtime-monitor/pcsc/device'], resolve),
        name: 'device',
        meta: { title: '设备列表' }
      },
      {
        path: 'channelList',
        component: (resolve) => require(['@/views/cms/realtime-monitor/pcsc/channelList'], resolve),
        name: 'channelList',
        meta: { title: '设备通道列表' }
      },
      {
        path: 'player',
        component: (resolve) => require(['@/views/cms/realtime-monitor/monitor6c/player'], resolve),
        name: 'player',
        meta: { title: '视频播放' }
      },
      {
        path: 'videoRecord',
        component: (resolve) => require(['@/views/cms/realtime-monitor/monitor6c/videoRecord'], resolve),
        name: 'videoRecord',
        meta: { title: '设备录像' }
      }
    ]
  },
  {
    path: '/cms/product',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'detail',
        component: (resolve) => require(['@/views/cms/realtime-monitor/pcsc/components/ProductDetail'], resolve),
        name: 'detail',
        meta: { title: '产品详情' }
      },
      {
        path: 'index',
        component: (resolve) => require(['@/views/cms/realtime-monitor/pcsc/product'], resolve),
        name: 'index',
        meta: { title: '产品' }
      }
    ]
  },
  {
    path: '/cms/rule',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'detail',
        component: (resolve) => require(['@/views/cms/realtime-monitor/rule/components/ruleDetail'], resolve),
        name: 'detail',
        meta: { title: '规则联动详情' }
      },
      {
        path: 'index',
        component: (resolve) => require(['@/views/cms/realtime-monitor/rule/index'], resolve),
        name: 'index',
        meta: { title: '规则联动' }
      }
    ]
  },
  {
    path: '/cms/index',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'defectInfo',
        component: (resolve) => require(['@/views/cms/analysis-management/defect-management/defectInfo'], resolve),
        name: 'defectInfo',
        meta: { title: '缺陷信息' }
      },
      {
        path: 'detailedPlanManagement',
        component: (resolve) => require(['@/views/cms/check-management/check-plan/DetailedPlanManagement'], resolve),
        name: 'detailedPlanManagement',
        meta: { title: '详细计划管理' }
      }
    ]
  },
  {
    path: '/cmsIndex',
    component: Layout,
    redirect: '/cms/defect-statistics',
    hidden: true,
    children: [
      // {
      //   path: '/cms/defect-statistics',
      //   name: 'DefectStatistics',
      //   component: () => import('@/views/cms/data-statistics/defect-statistics'),
      //   // component: () => import('@/views/cms/Inspection-management/Check-data-management/CheckDataOverview'),
      //   meta: { title: '缺陷统计', affix: true },
      //   hidden: true
      // }
      // {
      //   path: 'welcome',
      //   component: Welcome,
      //   name: 'Welcome',
      //   meta: { title: '首页', affix: true }
      // }
      // ,
      // {
      //   path: 'gis/cluster',
      //   component: () => import('@/views/gis/cluster'),
      //   name: 'Cluster',
      //   meta: { title: 'GIS页面', affix: true }
      // }
    ]
  }

  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]

export const asyncRoutes = []

const createRouter = () => new Router({
  base: window.__POWERED_BY_QIANKUN__ ? '/cms' : '/',
  // base: window.__POWERED_BY_QIANKUN__ ? '/jcwsc' : process.env.BASE_URL,
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router

// 页面刷新后重新设置权限页面动态路由，防止出现动态路由404问题
// const reSetPermissionList = to => {
//   return new Promise((resolve, reject) => {
//     if (to.path !== '/login' && this.$store.state.permission.permissionList.length === 0) {
//       this.$store
//         .dispatch('permission/getPermissionList')
//         .then(() => {
//           resolve('permCode');
//         })
//         // eslint-disable-next-line handle-callback-err
//         .catch(error => {
//           resolve('permCode');
//         });
//     } else {
//       resolve();
//     }
//   });
// };
// router.beforeEach((to, from, next) => {
//   const accessToken = localStorage.getItem('accessToken');
//   // eslint-disable-next-line no-undef
//   if (_.isEmpty(accessToken)) { // 是否已经登录 否 去登陆页面
//     next({
//       path: '/login',
//       query: {
//         redirect: to.fullPath
//       }
//     });
//   } else { // 已登录用户进入页面
//     if (to.path === '/login') {
//       next({ path: '/' });
//     } else {
//       reSetPermissionList(to).then(data => {
//         data === 'permCode' ? next({ path: to.path, query: to.query }) : next();
//       });
//     }
//   }
// });
